package dto

import (
	"github.com/samber/lo"
	"html/template"
	"monoblog/domain/model"
	"time"
)

func ConvertWebUserToUserResp(user *model.WebUser, i int) *UserResp {
	if user == nil {
		return nil
	}
	return &UserResp{
		UID:        user.Username,
		Phone:      user.Phone,
		Email:      user.Email,
		AvatarUrl:  user.AvatarUrl,
		Nickname:   user.Nickname,
		Profile:    user.Profile,
		Score:      user.Score,
		Status:     user.Status,
		Roles:      user.Roles,
		CreateTime: user.CreateTime,
	}
}

func ConvertArticleToBlogDoc(obj *model.Article, i int) *model.BlogDoc {
	if obj == nil {
		return nil
	}

	blogDoc := &model.BlogDoc{
		BlogId:       obj.ID,
		Title:        obj.Title,
		CategoryId:   obj.CategoryId,
		CategoryName: obj.CategoryName,
		ShowText:     obj.ShowText,
		Photo:        obj.Photo,
		AuthorId:     obj.AuthorId,
		AuthorName:   obj.AuthorName,
		AuthorAvatar: obj.AuthorAvatar,
		PublishTime:  obj.PublishTime.Unix(),
	}
	if obj.BlogStats != nil {
		blogDoc.ClickCount = obj.BlogStats.ClickCount
		blogDoc.CommentCount = obj.BlogStats.CommentCount
		blogDoc.LikeCount = obj.BlogStats.LikeCount
		blogDoc.FavoriteCount = obj.BlogStats.FavoriteCount
	}

	return blogDoc
}

func ConvertBlogDocToArticle(obj *model.BlogDoc, i int) *model.Article {
	if obj == nil {
		return nil
	}

	article := &model.Article{
		ID:           obj.BlogId,
		Title:        obj.Title,
		CategoryId:   obj.CategoryId,
		CategoryName: obj.CategoryName,
		ShowText:     obj.ShowText,
		Photo:        obj.Photo,
		AuthorId:     obj.AuthorId,
		AuthorName:   obj.AuthorName,
		AuthorAvatar: obj.AuthorAvatar,
		PublishTime:  lo.ToPtr(time.Unix(obj.PublishTime, 0)),
		BlogStats: &model.BlogStats{
			ClickCount:    obj.ClickCount,
			CommentCount:  obj.CommentCount,
			LikeCount:     obj.LikeCount,
			FavoriteCount: obj.FavoriteCount,
		},
	}

	if obj.LightTitle != "" { // 如果高亮标题不为空，替换标题
		article.RenderedTitle = template.HTML(obj.LightTitle)
	} else {
		article.RenderedTitle = template.HTML(obj.Title)
	}
	if obj.LightText != "" { // 如果高亮文本不为空，替换显示文本
		article.RenderedShowText = template.HTML(obj.LightText)
	} else {
		article.RenderedShowText = template.HTML(obj.ShowText)
	}

	return article
}
